package ict;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import ict.mobile.R;
import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.*;
import android.text.format.DateFormat;
import android.view.*;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class createNewOrder extends Activity implements
AdapterView.OnItemSelectedListener{
	

	SQLiteDatabase db;
	String sql;
	Cursor cursor = null;
	String[] columns = { "mid", "name", "password", "age" };
	String dataStrHeader = String.format("%4s %-12s %-9s %3s\n", "Mid", "Name",
			"Password", "Age");
	String dataStr;
	EditText updateQuantityET;
	TextView newOrderIDTV;
	
	String loginName, StaffID;
	TextView newOrderPLabel1, newOrderPLabel2, newOrderPLabel3, newOrderPLabel4, newOrderPLabel5;
	TextView newOrderPrice1, newOrderPrice2, newOrderPrice3, newOrderPrice4, newOrderPrice5;
	TextView newOrderLoginNameTV;
	TextView priceED1, priceED2, priceED3, priceED4, priceED5;
	Button updateQuantityB;
	String pos;
	
	String result;

	ArrayList shopIDList = new ArrayList();
	Spinner shopSpinner;
	
	 public static final int createOrder = Menu.FIRST;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createorder);

		Bundle bunde = this.getIntent().getExtras();
		loginName = bunde.getString("loginName");
		
		findViewBy();
		newOrderLoginNameTV.setText(loginName);
		addShopCodeToList();
		addproductOrderIDToLabel();
		setProductLabel();
		
		 shopSpinner=(Spinner)findViewById(R.id.shopSpinner);
		shopSpinner.setOnItemSelectedListener(this);
		ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, shopIDList);
		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		shopSpinner.setAdapter(aa);
		

	}

	// ////find view by ID
	public void findViewBy() {
		newOrderIDTV = (TextView) findViewById(R.id.newOrderIDTV);
		
		newOrderPLabel1 = (TextView) findViewById(R.id.newOrderPLabel1);
		newOrderPLabel2 = (TextView) findViewById(R.id.newOrderPLabel2);
		newOrderPLabel3 = (TextView) findViewById(R.id.newOrderPLabel3);
		newOrderPLabel4 = (TextView) findViewById(R.id.newOrderPLabel4);
		newOrderPLabel5 = (TextView) findViewById(R.id.newOrderPLabel5);
		
		newOrderPrice1 = (TextView) findViewById(R.id.newOrderPrice1);
		newOrderPrice2 = (TextView) findViewById(R.id.newOrderPrice2);
		newOrderPrice3 = (TextView) findViewById(R.id.newOrderPrice3);
		newOrderPrice4 = (TextView) findViewById(R.id.newOrderPrice4);
		newOrderPrice5 = (TextView) findViewById(R.id.newOrderPrice5);
		
		
		newOrderLoginNameTV = (TextView) findViewById(R.id.newOrderLoginNameTV);
		priceED1 = (EditText) findViewById(R.id.priceED1);
		priceED2 = (EditText) findViewById(R.id.priceED2);
		priceED3 = (EditText) findViewById(R.id.priceED3);
		priceED4 = (EditText) findViewById(R.id.priceED4);
		priceED5 = (EditText) findViewById(R.id.priceED5);
		
	}

	////set Product label
	public void setProductLabel(){
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);
			ArrayList code = new ArrayList();
			ArrayList name = new ArrayList();;
			cursor = db.rawQuery("select * from Product;", null);
			// Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();

			while (cursor.moveToNext()) {
                String ProductCode = cursor.getString(0);
                String ProductName = cursor.getString(1);
                code.add(ProductCode);
                name.add(ProductName);
			}
			
			
			newOrderPLabel1.setText(""+code.get(0));
			newOrderPLabel2.setText(""+code.get(1));
			newOrderPLabel3.setText(""+code.get(2));
			newOrderPLabel4.setText(""+code.get(3));
			newOrderPLabel5.setText(""+code.get(4));
			
			newOrderPrice1.setText(""+name.get(0));
			newOrderPrice2.setText(""+name.get(1));
			newOrderPrice3.setText(""+name.get(2));
			newOrderPrice4.setText(""+name.get(3));
			newOrderPrice5.setText(""+name.get(4));
			
			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}
		
	}
	
	// /add shopCode to spinner
	public void addShopCodeToList() {
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			cursor = db.rawQuery("select * from Shop;", null);
			// Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();

			while (cursor.moveToNext()) {
				// or cursor.getInt(0);
				String ShopCode = cursor.getString(0);
				shopIDList.add(ShopCode);
			}
			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}
	}

	public void insertNewOrder(){
		
		
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.CREATE_IF_NECESSARY);
			
			
			///get time and date
			Calendar c = Calendar.getInstance();
	        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


			String OrderID = newOrderIDTV.getText().toString();
			String ShopCode = shopSpinner.getSelectedItem().toString();
			//String StaffID
			String lg = newOrderLoginNameTV.getText().toString();
			String date = df.format(c.getTime());

			checkFiller(priceED1.getText().toString());
			priceED1.setText(getCheckFiller());
			String prod01 = priceED1.getText().toString();
			
			checkFiller(priceED2.getText().toString());
			priceED2.setText(getCheckFiller());
			String prod02 = priceED2.getText().toString();
			
			checkFiller(priceED3.getText().toString());
			priceED3.setText(getCheckFiller());
			String prod03 = priceED3.getText().toString();
			
			checkFiller(priceED4.getText().toString());
			priceED4.setText(getCheckFiller());
			String prod04 = priceED4.getText().toString();
			
			checkFiller(priceED5.getText().toString());
			priceED5.setText(getCheckFiller());
			String prod05 = priceED5.getText().toString();			
						
			
			sql = "INSERT INTO productOrder(OrderID, ShopCode, StaffID, date, LoginName, prod01 ,prod02, prod03 ,prod04 ,prod05) values"
					+ "('"+ OrderID +"', '" + ShopCode + "', "+StaffID+", '" + date + "', '"+ loginName +"', "+ prod01 +", "+ prod02+ ", "+ prod03 +", "+ prod04 +", "+ prod05+"); ";
			db.execSQL(sql);
			
			db.close();
		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), 1).show();
		}
		
		
	}
	
	public void checkFiller(String target){
		if(target==null||target.equals("")){
			target = "0";
		}
		
		result = target;
	}
	
	public String getCheckFiller(){
		
		return result;
	}
	
	
	// ////Display All of the database
	public void selectAllFromStaff(){
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			cursor = db.rawQuery("select * from Staff Where LoginName = '"+ loginName +"'", null);
			
			String dataStr = null;
			while (cursor.moveToNext()) {
				int StaffID = cursor.getInt(cursor.getColumnIndex("StaffID"));
				dataStr += StaffID;
			}
			StaffID = dataStr;
			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}
	// /gen new orderID to label
	public void addproductOrderIDToLabel() {
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);
			int count =0 ;
			cursor = db.rawQuery("select * from productOrder;", null);
			// Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();

			while (cursor.moveToNext()) {
				// or cursor.getInt(0);
				count++;
			}
			newOrderIDTV.setText("order"+count);
			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	public void onItemSelected(AdapterView parent, View v, int position, long id){
		// TODO Auto-generated method stub
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

    public boolean onCreateOptionsMenu(Menu menu) 
    {
        menu.add(0, createOrder, 0, "Create Order.").setIcon(R.drawable.edit);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) 
    {
        super.onOptionsItemSelected(item);
        switch( item.getItemId() ) 
        {
            case createOrder:{
            	insertNewOrder();
            	break;
            }     		
        	 
           	 	
        }
        return true;
        }

}